const mysql = require('mysql')
const pool = mysql.createPool({
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: 'root',
    database: 'school_health'
})
//封装sql方法
const query = (sql, params, callback) => {
    //获取连接
    pool.getConnection((err, conn) => {
        if (err) {
            console.log('连接数据库失败')
            return
        }
        conn.query(sql, params, (err, result, fields) => {
            if (err) {
                conn.release()  //释放连接对象
                console.log('执行sql失败', err)
                return
            }
            callback(result, fields)
            conn.release() //释放连接对象
        })
    })
}


module.exports = query